package com.yiyahui.project.erp.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.yiyahui.common.utils.SecurityUtils;
import com.yiyahui.project.erp.domain.YiyaMarketReservation;
import com.yiyahui.project.erp.domain.requestVo.YiyaMarketVisitRequest;
import com.yiyahui.project.erp.domain.responseVo.YiyaMarketAndVisitVo;
import com.yiyahui.project.erp.service.IYiyaMarketReservationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import com.yiyahui.framework.aspectj.lang.annotation.Log;
import com.yiyahui.framework.aspectj.lang.enums.BusinessType;
import com.yiyahui.project.erp.domain.YiyaMarketVisit;
import com.yiyahui.project.erp.service.IYiyaMarketVisitService;
import com.yiyahui.framework.web.controller.BaseController;
import com.yiyahui.framework.web.domain.AjaxResult;
import com.yiyahui.common.utils.poi.ExcelUtil;
import com.yiyahui.framework.web.page.TableDataInfo;

/**
 * 市场回访Controller
 * 
 * @author yiyahui
 * @date 2020-05-08
 */
@Api("市场回访")
@RestController
@RequestMapping("/erp/visit")
public class YiyaMarketVisitController extends BaseController
{
    @Autowired
    private IYiyaMarketVisitService yiyaMarketVisitService;

    @Autowired
    private IYiyaMarketReservationService yiyaMarketReservationService;
 
    /**
     * 查询市场回访列表
     */
    @ApiOperation("查询市场回访列表")
    @PreAuthorize("@ss.hasPermi('erp:visit:list')")
    @GetMapping("/list")
    public TableDataInfo list(YiyaMarketVisitRequest yiyaMarketVisitRequest)
    {
        startPage();
        List<YiyaMarketVisit> list = yiyaMarketVisitService.selectYiyaMarketVisitList(yiyaMarketVisitRequest);
        return getDataTable(list);
    }

    /**
     * 导出市场回访列表
     */
    @ApiOperation("导出市场回访列表")
    @PreAuthorize("@ss.hasPermi('erp:visit:export')")
    @Log(title = "市场回访", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(YiyaMarketVisitRequest yiyaMarketVisitRequest)
    {
        List<YiyaMarketVisit> list = yiyaMarketVisitService.selectYiyaMarketVisitList(yiyaMarketVisitRequest);
        ExcelUtil<YiyaMarketVisit> util = new ExcelUtil<YiyaMarketVisit>(YiyaMarketVisit.class);
        return util.exportExcel(list, "visit");
    }

    /**
     * 获取市场回访详细信息
     */
    @ApiOperation("获取市场回访详细信息")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "Long", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:visit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yiyaMarketVisitService.selectYiyaMarketVisitById(id));
    }

    /**
     * 新增市场回访
     */
    @ApiOperation("新增市场回访")
    @ApiImplicitParam(name = "yiyaMarketVisit", value = "${tableComment}", dataType = "YiyaMarketVisit")
    @PreAuthorize("@ss.hasPermi('erp:visit:add')")
    @Log(title = "市场回访", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YiyaMarketVisit yiyaMarketVisit)
    {
        yiyaMarketVisit.setCreateBy(SecurityUtils.getUsername());
        return toAjax(yiyaMarketVisitService.insertYiyaMarketVisit(yiyaMarketVisit));
    }

    /**
     * 修改市场回访
     */
    @ApiOperation("修改市场回访")
    @ApiImplicitParam(name = "yiyaMarketVisit", value = "${tableComment}", dataType = "YiyaMarketVisit")
    @PreAuthorize("@ss.hasPermi('erp:visit:edit')")
    @Log(title = "市场回访", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YiyaMarketVisit yiyaMarketVisit)
    {
        return toAjax(yiyaMarketVisitService.updateYiyaMarketVisit(yiyaMarketVisit));
    }

    /**
     * 删除市场回访
     */
    @ApiOperation("删除市场回访")
    @ApiImplicitParam(name = "ids", value = "主键id数组", required = true, dataType = "Long[]", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:visit:remove')")
    @Log(title = "市场回访", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yiyaMarketVisitService.deleteYiyaMarketVisitByIds(ids));
    }


    /**
     * 查询市场回访记录列表
     */
    @ApiOperation("查询市场回访列表")
//    @PreAuthorize("@ss.hasPermi('erp:visit:list')")
    @GetMapping("/list/record")
    public TableDataInfo visitList(YiyaMarketVisitRequest yiyaMarketVisitRequest)
    {
        startPage();
        List<YiyaMarketAndVisitVo> yiyaMarketAndVisitVos = new ArrayList<>();
        List<YiyaMarketVisit> list = yiyaMarketVisitService.selectYiyaMarketVisitList(yiyaMarketVisitRequest);
        List<Long> mIds = list.stream().map(YiyaMarketVisit::getMarketReservationId).collect(Collectors.toList());
        Map<Long, YiyaMarketReservation> map = yiyaMarketReservationService.selectByIds(mIds).stream().collect(Collectors.toMap(YiyaMarketReservation::getId, a -> a, (k1, k2) -> k1));
        list.forEach(r -> {
            YiyaMarketAndVisitVo yiyaMarketAndVisitVo = new YiyaMarketAndVisitVo();
            yiyaMarketAndVisitVo.setYiyaMarketVisit(r);
            yiyaMarketAndVisitVo.setYiyaMarketReservation(map.get(r.getMarketReservationId()));
            yiyaMarketAndVisitVos.add(yiyaMarketAndVisitVo);
        });
        return getDataTable(yiyaMarketAndVisitVos,list);
    }

}
